﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLPKWeb.Models.LinQ.DanhMucChung;

using Webdiyer.WebControls.Mvc;
using DONGACS.Common;
using QLPKWeb.Models.FormBean.DanhMucChungModel;
using dm_LoaiDVTinh = QLPKWeb.Models.LinQ.DanhMucChung.dm_LoaiDVTinh;
using dm_NhaCC = QLPKWeb.Models.LinQ.DanhMucChung.dm_NhaCC;

namespace QLPKWeb.BussinessDelegate.DanhMucChung
{
    public class DonViDelegate
    {
        DBDanhMucChungDataContext DBDanhMucChung = new DBDanhMucChungDataContext();

        public IList<dm_LoaiDVTinh> GetAlldm_LoaiDVTinh()
        {
            return DBDanhMucChung.dm_LoaiDVTinhs.ToList();
        }


        public dm_LoaiDVTinh AddNewDonVi(dm_LoaiDVTinh dm_LoaiDVTinh)
        {
            DBDanhMucChung.dm_LoaiDVTinhs.InsertOnSubmit(dm_LoaiDVTinh);

            DBDanhMucChung.SubmitChanges();

            return GetDonViIdMax();
        }

        public void DeleteDonVi(string dm_LoaiDVTinhID)
        {
            dm_LoaiDVTinh dm_LoaiDVTinhNew =
                DBDanhMucChung.dm_LoaiDVTinhs.Single(p => p.Id == Convert.ToInt32(dm_LoaiDVTinhID));
            //dm_LoaiDVTinh.Id = dm_LoaiDVTinh.Id;

            DBDanhMucChung.dm_LoaiDVTinhs.DeleteOnSubmit(dm_LoaiDVTinhNew);
            DBDanhMucChung.SubmitChanges();
        }

        public void UpdateDonVi(dm_LoaiDVTinh dm_LoaiDVTinh)
        {
            dm_LoaiDVTinh dm_LoaiDVTinhUpdate =
                DBDanhMucChung.dm_LoaiDVTinhs.Single(p => p.Id == dm_LoaiDVTinh.Id);

            dm_LoaiDVTinhUpdate.Id = dm_LoaiDVTinh.Id;
            dm_LoaiDVTinhUpdate.Ten = dm_LoaiDVTinh.Ten;
           

            DBDanhMucChung.SubmitChanges();
        }

        public PagedList<dm_LoaiDVTinh> SearchDonViPagedList(int? id, string searchCriteria)// id = page Index
        {
            //IQueryable<dm_LoaiDVTinh> list = DBDanhMucChung.dm_LoaiDVTinhs.Where(p => p.Ten == searchCriteria);
            //DBDanhMucChung.dm_LoaiDVTinhs.ToPagedList(list,)

            return (PagedList<dm_LoaiDVTinh>)DBDanhMucChung.dm_LoaiDVTinhs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage).Where(p => p.Ten == searchCriteria);

        }

        public dm_LoaiDVTinh GetDonViById(string id)
        {
            try
            {
                dm_LoaiDVTinh item = this.DBDanhMucChung.dm_LoaiDVTinhs.Single(p => p.Id == Convert.ToInt32(id));
                return item;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public dm_LoaiDVTinh GetDonViByTen(string Ten)
        {
            //dm_LoaiDVTinh item = this.DBDanhMucChung.dm_LoaiDVTinhs.Where(p => p.Ten == ten).Count() != 0 ? this.DBDanhMucChung.dm_LoaiDVTinhs.Where(p => p.Ten == ten).ElementAt(0) : null ;

            IEnumerable<dm_LoaiDVTinh> list = this.DBDanhMucChung.dm_LoaiDVTinhs.Where(p => p.Ten == Ten);
            return list.Count() != 0 ? list.ElementAt(0) : null;
        }

        // get pl
    
        // end get

        public PagedList<dm_LoaiDVTinh> GetDonViPagedList(int? id)// id = page Index
        {
            return DBDanhMucChung.dm_LoaiDVTinhs.ToPagedList(id ?? 1, CommonVariables.PaggedItemsPerPage);
        }

        public dm_LoaiDVTinh BuildNewDonVi(DonViForm form)
        {
            dm_LoaiDVTinh newdm_LoaiDVTinh = new dm_LoaiDVTinh();
            newdm_LoaiDVTinh.Ten = form.TenDonVi;
         
            return newdm_LoaiDVTinh;
        }

        public dm_LoaiDVTinh BuildEditDonVi(DonViForm form)
        {
            dm_LoaiDVTinh newdm_LoaiDVTinh = new dm_LoaiDVTinh();
            newdm_LoaiDVTinh.Id = form.Id;
            newdm_LoaiDVTinh.Ten = form.TenDonVi;
            return newdm_LoaiDVTinh;
        }

        public dm_LoaiDVTinh GetDonViIdMax()
        {
            IList<dm_LoaiDVTinh> list = DBDanhMucChung.dm_LoaiDVTinhs.ToList();
            return list.Count != 0 ? list.ElementAt(list.Count - 1) : null;
        }
        public SelectList BuilUNIT(int? selectedValue)
        {
            IList<dm_LoaiDVTinh> selectNCC = DBDanhMucChung.dm_LoaiDVTinhs.OrderBy(p => p.Id).ToList();
            IList<object> selectList = new List<object>();
            int dem = 0;
            foreach (var o in selectNCC)
            {
                dem++;
                if (selectedValue != 0 || dem == selectedValue)
                {
                    selectList.Add(new { value = o.Id, Text = o.Ten, SelectedValue = selectedValue });
                }
                else
                {
                    selectList.Add(new { value = o.Id, Text = o.Ten });
                }
            }
            SelectList select = new SelectList(selectList, "Value", "Text", selectedValue);
            return select;
        }
    }
}